package com.tenglong.teacher.ui.adapter.viewholder;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.cuotibao.zhuoxiao.basics.GlobalConstant;
import com.cuotibao.zhuoxiao.basics.utils.IpUtils;
import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.cuotibao.zhuoxiao.basics.utils.glide.GlideOptions;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.StudentSignBottomInfo;
import com.tenglong.teacher.data.resultInfo.BaseStudentSignInfo;
import com.tenglong.teacher.data.resultInfo.base.BaseResult;
import com.tenglong.teacher.data.resultInfo.base.ResultVerifier;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.adapter.StudentAdapter;
import com.tenglong.teacher.ui.adapter.viewholder.base.BaseViewHolder;
import com.tenglong.teacher.ui.homepage.ClassroomRecordEvaluateActivity;
import com.tenglong.teacher.ui.homepage.EvaluatePreviewActivity;
import com.tenglong.teacher.ui.homepage.FragmentHompage;
import com.tenglong.teacher.ui.homepage.SignItemViewGroup;
import com.thinkcool.circletextimageview.CircleTextImageView;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class StudentViewHolder extends BaseViewHolder<BaseStudentSignInfo> implements View.OnClickListener {
    private CircleTextImageView image;
    private TextView name;
    private TextView sign_btn;
    private TextView classroomCase;
    private View iv_sele;
    private SignItemViewGroup rootView;
    private StudentSignBottomInfo data;
    private StudentAdapter.StudentListener listener;
    private Context mContext;
    private boolean isClass = true;

    public StudentViewHolder(View itemView) {
        super(itemView);
        this.mContext = itemView.getContext();
    }

    public StudentViewHolder(View itemView, StudentAdapter.StudentListener listener) {
        this(itemView);
        this.listener = listener;
        if (listener != null) {
            isClass = listener.getClassType().equals(FragmentHompage.TYPE_ALL_CLASS);
        }
        initView();
        setListeners();
    }

    private void setListeners() {
        rootView.setOnClickListener(this);
        sign_btn.setOnClickListener(this);
        classroomCase.setOnClickListener(this);
    }

    private void initView() {
        image = itemView.findViewById(R.id.image);
        name = itemView.findViewById(R.id.name);
        sign_btn = itemView.findViewById(R.id.sign_btn);
        classroomCase = itemView.findViewById(R.id.classroomCase);
        iv_sele = itemView.findViewById(R.id.iv_sele);
        rootView = itemView.findViewById(R.id.rootView);
        sign_btn.setEnabled(!isClass);
        sign_btn.setClickable(!isClass);
    }

    @Override
    public void setLayoutInfo(BaseStudentSignInfo info) {
        this.data = (StudentSignBottomInfo) info;
        RequestOptions options = GlideOptions.getGlideRequestOptions().
                error(R.drawable.setting_image_defauft_header).
                placeholder(R.drawable.header_image_loading);
        Glide.with(mContext)
                .load(IpUtils.getImageUrl(data.photoUrl))
                .apply(options)
                .into(image);
        String niceName = data.name == null ? "" : data.name;
        name.setText(data.stuState == 2 ? niceName + "(" + "试听" + ")" : niceName);
        name.setTextColor(Color.parseColor(data.stuState == 2 ? "#fc4c24" : "#333333"));
        iv_sele.setVisibility(data.isShowCircleQuan ? View.VISIBLE : View.GONE);
        rootView.setChildIsClick(data.isShowCircleQuan);
        iv_sele.setSelected(data.isSele);
        setSignState();
        setEvaluateState();
    }

    private void setEvaluateState() {
        if (data.type == 2 || data.type == 0 || data.type == 1) {
            classroomCase.setEnabled(true);
            classroomCase.setClickable(true);
            classroomCase.setBackgroundResource(R.drawable.student_sign_bg_green_shape);
        } else {
            classroomCase.setEnabled(false);
            classroomCase.setClickable(false);
            classroomCase.setBackgroundResource(R.drawable.student_sign_bg_gray_shape);
        }
    }

    private void setSignState() {
        switch (data.type) {
            case 0:
                sign_btn.setText(isClass ? "未签到" : "签到");
                sign_btn.setBackgroundResource(R.drawable.student_sign_bg_green_shape);
                break;
            case 1://迟到
                sign_btn.setEnabled(false);
                sign_btn.setClickable(false);
                sign_btn.setText("迟到");
                sign_btn.setBackgroundResource(R.drawable.student_sign_bg_red_shape);
                break;
            case 3://请假
                sign_btn.setEnabled(false);
                sign_btn.setClickable(false);
                sign_btn.setText("请假");
                sign_btn.setBackgroundResource(R.drawable.student_sign_bg_red_shape);
                break;
            case 2://正常
                sign_btn.setEnabled(false);
                sign_btn.setClickable(false);
                sign_btn.setText("已签到");
                sign_btn.setBackgroundResource(R.drawable.student_sign_bg_gray_shape);
                break;
            case 7://缺勤
                sign_btn.setEnabled(false);
                sign_btn.setClickable(false);
                sign_btn.setText("缺勤");
                sign_btn.setBackgroundResource(R.drawable.student_sign_bg_red_shape);
                break;
            case 8://未知
                sign_btn.setEnabled(false);
                sign_btn.setClickable(false);
                sign_btn.setText("未知");
                sign_btn.setBackgroundResource(R.drawable.student_sign_bg_gray_shape);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.classroomCase:
                if (!data.isClassBeing) {
                    ToastUtils.showCenterToast("上课后才能评论");
                    return;
                }
                if (data.type == 0) {
                    ToastUtils.showCenterToast("点击签到后才可评论");
                    return;
                }
                listener.onRecordPosition(getLayoutPosition());
                Intent intent = new Intent();
                intent.setClass(mContext, data.isEvaluate ? EvaluatePreviewActivity.class : ClassroomRecordEvaluateActivity.class);
                intent.putExtra(data.isEvaluate ? EvaluatePreviewActivity.KEY_PREVIEW_DATA : ClassroomRecordEvaluateActivity.KEY_CLASSROOM_DATA, data);
                intent.putExtra(EvaluatePreviewActivity.KEY_DATA_RESOURCE, data.isEvaluate ? EvaluatePreviewActivity.RESOURCE_STUDENT_SIGN : EvaluatePreviewActivity.RESOURCE_CLASS_ROOM_RECORDE);
                ((Activity) mContext).startActivityForResult(intent, GlobalConstant.REQUEST_CODE_CLASS_ROOM);
                break;
            case R.id.sign_btn:
                singleSign();
                break;
            case R.id.rootView:
                if (data.type != 0) return;
                if (data.isShowCircleQuan) {
                    iv_sele.setSelected(data.isSele = !data.isSele);
                }
                break;
        }
    }

    private void singleSign() {
        if (!data.isClassBeing) {
            ToastUtils.showCenterToast("上课后才能签到");
            return;
        }
        final ProgressDialog mProgressDlg = new ProgressDialog(mContext);
        mProgressDlg.setCanceledOnTouchOutside(false);
        mProgressDlg.setMessage("正在访问,请稍后...");
        mProgressDlg.show();
        CreateRequestEntity.getWebService().singleStudentSign(data.id, "2", data.classRecordId).enqueue(new Callback<BaseResult<String>>() {
            @Override
            public void onResponse(Call<BaseResult<String>> call, Response<BaseResult<String>> response) {
                mProgressDlg.dismiss();
                if (ResultVerifier.isSucceed(response)) {
                    data.type = 2;
                    setSignState();
                    if (listener != null) {
                        listener.refreshPeopleNum();
                    }
                } else {
                    ToastUtils.showCenterToast(ResultVerifier.serceErrers(response));
                }
            }

            @Override
            public void onFailure(Call<BaseResult<String>> call, Throwable t) {
                mProgressDlg.dismiss();
                ResultVerifier.systerErrers(t);
            }
        });
    }
}
